package com.dcxm.stu.dao.impl

import com.dcxm.stu.bean.HomeWork
import com.dcxm.stu.dao.HomeWorkDao
import com.dcxm.stu.rowmapper.HomeWorkRowMapper
import org.intellij.lang.annotations.Language
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.stereotype.Repository

@Repository
class HomeWorkDaoI : HomeWorkDao {

    @Autowired
    private lateinit var jdbcTemplate: JdbcTemplate

    /**
     * @author youngxhui
     * 插入(增加)作业
     * @param homeWork 作业
     * @param path 路径
     * @return row 是否成功
     */
    override fun insertHomeWork(homeWork: HomeWork, path: String): Int {
        val sql = "INSERT INTO t_homework (name, content) VALUES (?,?)"
        return jdbcTemplate.update(sql, homeWork.name, homeWork.content)
    }

    override fun insertPath(path: String, id: Int): Int {
        @Language("MySQL")
        val sql = "INSERT INTO t_path (path, homework_id) VALUES (?,?)"
        return jdbcTemplate.update(sql, path, id)

    }

    /**
     * @author youngxhui
     * 删除作业
     * @param id 要删除的 id
     * @return Int 返回 1 or 0
     */
    override fun deleteHomeWork(id: String): Int {
        @Language("MySQL")
        val sql = "DELETE FROM t_homework WHERE id = ?"
        return jdbcTemplate.update(sql, id)
    }

    /**
     * @author youngxhui
     * 查询所有作业
     * @return list 返回作业列表
     */
    override fun listHomeWork(): List<HomeWork> {
        @Language("MySQL")
        val sql = "SELECT * FROM t_homework"
        val homeWorkList = jdbcTemplate.query(
                sql,
                HomeWorkRowMapper()
        )
        return homeWorkList
    }

    /**
     * @author youngxhui
     * 获取指定id的作业列表
     * @param id 要获取的作业 id
     * @return homework 返回作业
     */
    override fun listSelectHomeWorkById(id: String): HomeWork {
        @Language("MySQL")
        val sql = "SELECT * FROM t_homework WHERE id = ?"
        val params = arrayOf(id)
        val homeWork = jdbcTemplate.queryForObject(sql,
                params, HomeWorkRowMapper())
        return homeWork
    }

    /**
     * @author youngxhui
     * 获取指定类型的作业列表
     * @param type 获取的类型
     * @return 返回作业列表
     */
    override fun listSelectHomeWorkByType(type: String): List<HomeWork> {
        @Language("MySQL")
        val sql = "SELECT * FROM t_homework WHERE type = ?"
        val homeWork = jdbcTemplate.query(sql, HomeWorkRowMapper(), type)
        return homeWork
    }

    /**
     * @author youngxhui
     * 修改 id 修改作业
     * @param homeWork 作业
     * @return 是否删除成功
     */
    override fun updateHomeWork(homeWork: HomeWork): Int {
        @Language("MySQL")
        val sql = "UPDATE t_homework SET name = ? AND content = ? AND type = ? WHERE id = ?"
        return jdbcTemplate.update(sql, homeWork.name, homeWork.content, homeWork.type, homeWork.id)
    }
}